package com.sogoodsoft.service.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sogoodsoft.util.InfosysUtils;

public class ComputerInfo {
	private static Logger logger = LoggerFactory.getLogger(ComputerInfo.class);
	private final static String salt = "sogoodsoftXY";
	private static String os;
	private static String boot;
	private final static long len = 254023;
	static {
		init();
	}

	/**
	 * Read HardDisk Infomation
	 * 
	 * @return WDC WD3200BEVT-22ZCT0 11.01A11 WD-WXH309345239
	 */
	public static native String showHDSerial();

	public static String MD5(String str) {
		String md5 = "";
		md5 = DigestUtils.md5Hex(str + salt);
		return md5;
	}

	public static String specialCode() {
		String code = "";
		try {
			String str = showHDSerial();
			code = MD5(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return code;
	}

	public static void init() {
		boot = System.getProperty("sun.boot.library.path");
		os = System.getProperty("os.name");
		File dfile;
		File bin;
		try {
			if (os.contains("Windows")) {
				String dll = "classes/SGS.dll";
				dfile = new File(InfosysUtils.translatePath(dll));
				bin = new File(boot + File.separator + "SGS.dll");
				if (!bin.exists()) {
					if (!dfile.exists())
						throw new Exception("文件丢失！");
					dfile = new File(InfosysUtils.translatePath(dll));
					bin.createNewFile();
					FileInputStream fin = new FileInputStream(dfile);
					FileOutputStream fout = new FileOutputStream(bin);
					byte buffer[] = new byte[1000];
					int length;
					while ((length = fin.read(buffer)) > 0) {
						fout.write(buffer, 0, length);
					}
					fin.close();
					fout.close();
					dfile.delete();
					//bin = new File(boot + File.separator + "SGS.dll");
					//System.out.println(bin.length());
					if (bin.length() != len)
						throw new Exception("文件篡改异常！");
					
				}
				System.loadLibrary("SGS");
			}
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

	}

	public static void main(String[] args) {
		System.out.println(specialCode());
		String str[] = new String[10];
		System.out.println(str.getClass().isArray());
		System.out.println("tt".getClass().getName());
	}
}
